package gLib::SRV;

## ----------------------------------------------------
## Title :			Generic Server Class
## Description :	Provide generic function to connect to any Server
## Date :			2013-07-11
## Version :		0.1
## Author :			@TiTom
## ----------------------------------------------------

## ----------------------------------------------------
## Todo List
## ----------------------------------------------------
## - Add Perl Doc comments
## - Validate all functions
## - Insert function from gDebug for debug actions.
## ----------------------------------------------------


########################################################
#	Requirements
########################################################

## Required Library
use strict;
use warnings;
use Net::HTTP;
use HTTP::Client;
use gLib::gDebug;
use IO::Socket;

########################################################
#	FUNCTIONS
########################################################

## Class Constructor
sub new {
	my ($class) = @_;

	my $dbst = new gLib::gDebug;
	$dbst->debug(1);
	my $self = {
		IP		=> undef,
		PORT	=> "80",
		NAME	=> "gLib::SRV Instance",
		HEADERS	=>	undef,
		CONTENT	=>	undef,
		DEFAULT_URL	=> undef,
		DBST		=> $dbst
	};

	bless	$self,$class;
	return	$self;
}

## Function to Get IP informations
sub ip {
	my ( $self, $my_ip ) = @_;
	$self->{IP} = $my_ip if defined($my_ip);
	return $self->{IP};
}
## Function to Get Port informations
sub port {
	my ( $self, $my_port ) = @_;
	$self->{PORT} = $my_port if defined($my_port);
	return $self->{PORT};
}

## Function to Get Name Server informations
sub name {
	my ( $self, $my_name ) = @_;
	$self->{NAME} = $my_name if defined($my_name);
	return $self->{NAME};
}

## Function to Send GET request
## Return 0 if connection is KO
## Return HTTP Code (Number - Message) otherwise
sub send_get {
	my ( $self , $my_url ) = @_;
	
	## Define User Agent
	my $client = HTTP::Client->new("gLibBot/0.1");
	
	## If my_url is not set, use default url
	if( not defined($my_url))
	{
		$self->{DBST}->log("gLib::SRV->send_get - URL not defined - using default value");
		$my_url = $self->{DEFAULT_URL};
	}

	## Check if port is open on our server.
	if( $self->check_port == 0)
	{
		$self->{DBST}->log("gLib::SRV->send_get - Port unreachable");
		return "0";
	}
	## Delete last \n from the url
	chomp($my_url);
	
	## Send Request and retrieve result
	my $site = $client->get($my_url);

	## Save headers for futur use
	my @headers = $client->response_headers;
	push(@{$self->{HEADERS}},@headers);

	## Save content to our object
	$self->{CONTENT} = $site;

	## Clean Result code to remove \n at the end of the string
	my $bad_string = $client->status_message;
	my $clean_string = gLib::gLib->string_clean($bad_string);

	return $clean_string;
}

## Function to send Headers to our main program
## Return Array with all headers receive during last query.
sub get_headers {
	my ( $self ) = @_;
	my @headers = @{$self->{HEADERS}};
	return @headers;
}

## Function to send back page content from previous Web Request
## Return content
sub get_content {
	my( $self ) = @_;
	return $self->{CONTENT};
}


## Function to test Port availability
## return 0 if connection is KO
## return 1 if connection is OK
sub check_port {
	my ( $self ) = @_;
	
	my $sock = new IO::Socket::INET( 
		PeerAddr => $self->{IP},
		PeerPort => $self->{PORT},
		Timeout	=> 1,
		Proto => 'tcp'
	);
	## Check $sock
	if($sock)
	{
		close $sock;
		return 1;
	}
	else {
		#$self->{DBST}->log("gLib::gLib->check_port - SOCK KO");
		return 0;
	}
}
1;
